﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Schema;

namespace StockPoint.Util
{
    public class ValidUtil
    {
        private static string ErrorMsg = string.Empty;

        #region " Xml "

        public static string XmlStringFromSchema(string sXml, string sXsdPath)
        {
            ErrorMsg = string.Empty;
            XmlReader validator = null;

            try
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationType = ValidationType.Schema;

                XmlSchemaSet schemas = new XmlSchemaSet();
                settings.Schemas = schemas;

                schemas.Add(null, sXsdPath);
                settings.ValidationEventHandler += ValidationHandler;

                validator = XmlReader.Create(new StringReader(sXml), settings);
                while (validator.Read()) ;
            }
            catch (Exception ex)
            {
                ErrorMsg = ex.Message;
            }
            finally
            {
                if (validator != null) validator.Close();
            }

            return ErrorMsg;
        }

        private static void ValidationHandler(object sender, ValidationEventArgs args)
        {
            ErrorMsg = args.Message;
        }

        #endregion

        #region " String "

        /// <summary>
        /// 특수 문자 확인
        /// </summary>
        public static bool StringSpecialChar(string sValue)
        {
            if (sValue == null)
                return false;

            if (sValue.IndexOf("|") > -1 || sValue.IndexOf("^") > -1 || sValue.IndexOf("$") > -1 || sValue.IndexOf("*") > -1)
                return false;

            return true;
        }

        /// <summary>
        /// 문자 확인 - 최대 길이
        /// </summary>
        public static bool StringLengthMax(string sValue, int iLength)
        {
            if (sValue == null)
                return false;

            if (sValue.Length > iLength)
                return false;

            return true;
        }

        /// <summary>
        /// 문자 확인 - 최소 길이
        /// </summary>
        public static bool StringLengthMin(string sValue, int iLength)
        {
            if (sValue == null)
                return false;

            if (sValue.Length < iLength)
                return false;

            return true;
        }

        /// <summary>
        /// 문자 확인 - 동일한 길이
        /// </summary>
        public static bool StringLengthEqual(string sValue, int iLength)
        {
            if (sValue == null)
                return false;

            if (sValue.Length != iLength)
                return false;

            return true;
        }

        /// <summary>
        /// 숫자 확인 - 범위
        /// </summary>
        public static bool StringRange(string sValue, int iStart, int iEnd)
        {
            if (sValue == null)
                return false;

            if (iStart != -1 && sValue.Length < iStart)
                return false;

            if (iEnd != -1 && sValue.Length > iEnd)
                return false;

            return true;
        }

        /// <summary>
        /// 문자 확인 - 정해진 값.
        /// </summary>
        public static bool StringEnums(string sValue, string sEnums)
        {
            if (sValue == null)
                return false;

            string[] values = System.Text.RegularExpressions.Regex.Split(sEnums, "\\,");
            if (values.Length < 1)
                return false;

            bool found = false;
            foreach (string val in values)
            {
                if (sValue.Equals(val))
                    found = true;
            }

            return found;
        }


        #endregion

        #region " Integer "

        /// <summary>
        /// 숫자 확인
        /// </summary>
        public static bool Integer(string sValue)
        {
            sValue = sValue.Replace(",", "");
            return IntegerRange(sValue, -1, -1);
        }

        /// <summary>
        /// 숫자 확인 - 범위
        /// </summary>
        public static bool IntegerRange(string sValue, int iStart, int iEnd)
        {
            int iParseValue = 0;
            sValue = sValue.Replace(",", "");

            if (sValue == null)
                return false;

            if (int.TryParse(sValue, out iParseValue) == false)
                return false;

            if (iStart != -1 && iParseValue < iStart)
                return false;

            if (iEnd != -1 && iParseValue > iEnd)
                return false;

            return true;
        }

        public static bool IntegerEnums(string sValue, string sEnums)
        {
            int iParseValue = 0;
            sValue = sValue.Replace(",", "");

            if (sValue == null)
                return false;

            if (int.TryParse(sValue, out iParseValue) == false)
                return false;

            string[] values = System.Text.RegularExpressions.Regex.Split(sEnums, "\\,");
            if (values.Length < 1)
                return false;

            bool found = false;
            foreach (string val in values)
            {
                if (Integer(val) == false)
                    return false;
                if (iParseValue == Convert.ToInt32(val))
                    found = true;
            }

            return found;
        }

        /// <summary>
        /// 숫자(소숫점) 확인
        /// </summary>
        public static bool ValidDecimal(string sValue)
        {
            sValue = sValue.Replace(",", "");
            return ValidDecimalRange(sValue, -1, -1);
        }

        /// <summary>
        /// 숫자(소숫점) 확인 - 범위
        /// </summary>
        public static bool ValidDecimalRange(string sValue, int iStart, int iEnd)
        {
            decimal dParseValue = 0;
            sValue = sValue.Replace(",", "");

            if (sValue == null)
                return false;

            if (decimal.TryParse(sValue, out dParseValue) == false)
                return false;

            if (iStart != -1 && dParseValue < iStart)
                return false;

            if (iEnd != -1 && dParseValue > iEnd)
                return false;

            return true;
        }

        #endregion

        #region " DateTime "

        /// <summary>
        /// 일자 확인
        /// </summary>
        public static bool DateTimeDate(string sValue)
        {
            DateTime dtResult;

            if (string.IsNullOrEmpty(sValue))
                return false;

            if (sValue.Length != 10)
                return false;

            if (System.DateTime.TryParse(sValue, out dtResult) == false)
                return false;

            return true;
        }

        /// <summary>
        /// 시간 확인
        /// </summary>
        public static bool DateTimeHour(string sValue)
        {
            return IntegerRange(sValue, 0, 23);
        }

        #endregion

        #region " Regular Expression "

        /// <summary>
        /// Validate regular expression.
        /// </summary>
        private static bool RegValid(string pattern, string value)
        {
            Regex regex = new Regex(pattern);
            return regex.IsMatch(value);
        }

        /// <summary>
        /// Phone
        /// </summary>
        public static bool RegPhone(string value)
        {
            string pattern = @"^\d{3}-\d{3,4}-\d{4}$";
            return RegValid(pattern, value);
        }

        /// <summary>
        /// Url
        /// </summary>
        public static bool RegUrl(string value)
        {
            string pattern = @"^((http)://[a-z0-9-]+.[][a-zA-Z0-9:&#@=_~%;?/.+-]+)$";
            return RegValid(pattern, value);
        }

        /// <summary>
        /// Email
        /// </summary>
        public static bool RegEmail(string value)
        {
            string pattern = @"^[a-z]+[a-z0-9_-]*(([.]{1})|([a-z0-9_-]*))[a-z0-9_-]+[@]{1}[a-z0-9_-]+[.](([a-z]{2,3})|([a-z]{3}[.]{1}[a-z]{2}))$";
            return RegValid(pattern, value);
        }

        /// <summary>
        /// IP
        /// </summary>
        public static bool RegIp(string value)
        {
            string pattern = @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$";
            return RegValid(pattern, value);
        }

        /// <summary>
        /// *를 포함한 IP 정규식 검사
        /// </summary>
        /// <remarks>
        /// e.g) 10.1.10.*<br/>
        /// e.g) 10.*.10.*
        /// </remarks>s
        public static bool RegIpWithStar(string value)
        {
            string pattern = @"^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])|\*)\.((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])|\*)\.((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])|\*)\.((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])|\*)$";
            return RegValid(pattern, value);
        }

        /// <summary>
        /// 날짜형식 (yyyy-MM-dd)
        /// </summary>
        public static bool RegDateFormat1(string value)
        {
            string pattern = @"^(19[0-9]{2}|2[0-9]{3})-(0[1-9]|1[012])-([123]0|[012][1-9]|31)$";
            return RegValid(pattern, value);
        }

        /// <summary>
        /// 날짜형식 (yyyyMMdd)
        /// </summary>
        public static bool RegDateFormat2(string value)
        {
            string pattern = @"^(19[0-9]{2}|2[0-9]{3})(0[1-9]|1[012])([123]0|[012][1-9]|31)$";
            return RegValid(pattern, value);
        }

        /// <summary>
        /// 날짜형식 (yyyy-MM)
        /// </summary>
        public static bool RegDateFormat3(string value)
        {
            string pattern = @"^(19[0-9]{2}|2[0-9]{3})-(0[1-9]|1[012])$";
            return RegValid(pattern, value);
        }

        #endregion

    }
}